<template>
  <div class="mod-form-model">
    <el-form
      :inline="true"
      size="small"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.name"
          placeholder="表单模型名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      size="mini"
      border
      row-key="id"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>

      <el-table-column type="index" align="center" label="编号" width="100" />

      <el-table-column
        prop="name"
        header-align="center"
        align="left"
        label="模型名称"
      >
      </el-table-column>

      <el-table-column
        prop="dataState"
        header-align="center"
        align="center"
        width="80"
        label="模型状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.dataState === 0" size="small">启用</el-tag>
          <el-tag
            v-else-if="scope.row.dataState === -1"
            size="small"
            type="danger"
            >冻结</el-tag
          >
        </template>
      </el-table-column>

      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="180"
        label="创建时间"
      >
      </el-table-column>

      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="备注"
      >
      </el-table-column>

      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="200"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row, 1)"
            >添加下级</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row, 2)"
            >修改</el-button
          >

          <el-button
            v-if="scope.row.dataState === -1"
            type="text"
            size="small"
            @click="enableHandle(scope.row)"
            >启用</el-button
          >
          <el-button
            v-if="scope.row.dataState === 0"
            type="text"
            size="small"
            @click="disableHandle(scope.row)"
            >冻结</el-button
          >

          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./formmodel-add-or-update";
import {
  formModelPageList,
  formModelDeleteEvent,
  formModelEnableEvent,
  formModelDisableEvent,
} from "@/api/flow";
import { treeDataTranslate } from "@/utils";

export default {
  data() {
    return {
      dataForm: {
        name: "",
      },
      dataList: [],
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
    };
  },
  components: {
    AddOrUpdate,
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      formModelPageList({
        name: this.dataForm.name,
      }).then((res) => {
        if (res && res.code === 0) {
          this.dataList = treeDataTranslate(res.data, "id");
        }
        this.dataListLoading = false;
      });
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(data, addChildren) {
      this.addOrUpdateVisible = true;
      if (addChildren) {
        data.addChildren = addChildren;
      }
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(data);
      });
    },
    enableHandle(row) {
      this.$confirm(`确定进行启用操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          formModelEnableEvent({ id: row.id }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    disableHandle(row) {
      this.$confirm(`确定进行冻结操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          formModelDisableEvent({ id: row.id }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });

      this.$confirm(`是否确定进行[${id ? "删除" : "批量删除"}]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          formModelDeleteEvent({ ids: ids }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>
